<?php
/*
 * ebiz front
 *
 * TradeController
 *
 * @author allen
 *
 */
//require_once 'BaseController.php';
require_once '../app/core/controllers/BaseController.php';
require_once '../app/services/AAService.php';
require_once '../app/services/AgentAccountService.php';
require_once '../app/util/Pager.php';
require_once '../app/services/AgentAccountService.php';
require_once '../app/constants/ConfigConstants.php';
require_once '../app/constants/AgentAccount.php';
require_once "../". $GLOBALS["local"] ."/"."agentAccount.php";
require_once "../". $GLOBALS["local"] ."/"."myaccount.php";

class TradeController extends BaseController {

	public function index($param = array()){
		$v = $param['value'];
		echo "this is trade Controller indexAction! value is ".$v;
	}

	public function index1($param = array()){
		$v = $param['value'];
		echo "this is trade Controller indexAction1! value is ".$v;
	}

    /**
     * Make trade logs [trade_type = withdraw] list
     *
     * @author by M.Wang
     */
    public function withdrawlogs(){
    	self::tradelogslist( 3 );
    }

    /**
     * Make trade logs [trade_type = supplement] list
     *
     * @author by M.Wang
     */
    public function supplementlogs(){
    	self::tradelogslist( 1 );
    }

    /**
     * Select trade logs and display
     *
     * @author by M.Wang
     * @param int $int_tradetype
     */
    protected function tradelogslist( $int_tradetype ){
    	AAService::isLogin(true);
        $int_userid = AAService::getCustomer();

		$agentAccountService = new AgentAccountSercive();

        /**
         * Select data
         */
	    $arr_select = array(
	        'page' => HttpUtil::getPostString( 'page' ),
		    'sn' => HttpUtil::getPostString( 'sn' ),
		    'trade_num' => HttpUtil::getPostString( 'trade_num' ),
		    'createdd' => HttpUtil::getPostString( 'createdd' ),
		    'createdu' => HttpUtil::getPostString( 'createdu' ),
		    'payment_type' => HttpUtil::getPostString( 'payment_type' )
		);
		$str_otherWhere = '';
		if( !empty( $arr_select['sn'] ) )
		    $str_otherWhere.= " AND `a`.`sn` LIKE '%{$arr_select['sn']}%' ";
		if( !empty( $arr_select['trade_num'] ) )
		    $str_otherWhere.= " AND `a`.`trade_num` LIKE '%{$arr_select['trade_num']}%' ";
		if( !empty( $arr_select['createdd'] ) )
		    $str_otherWhere.= " AND `a`.`created` >= '{$arr_select['createdd']}' ";
		if( !empty( $arr_select['createdu'] ) )
		    $str_otherWhere.= " AND `a`.`created` <= '{$arr_select['createdu']}' ";
		if( !empty( $arr_select['payment_type'] ) )
		    $str_otherWhere.= " AND `a`.`" . ( $int_tradetype > 1 ? 'withdraw_type' : 'supplement_type' ) . "` = '{$arr_select['payment_type']}' ";

		/**
		 * Make page limit
		 */
		$int_limit = $GLOBALS['config']['items_per_page'];
		$int_nowPage = $arr_select['page'];
		if( $int_nowPage < 1 || empty( $int_nowPage ) )
		  $int_nowPage = 1;
		$int_countrows = 0;

		$arr_tradeLogs = $agentAccountService->getDataByTradeType( $int_tradetype, $int_nowPage - 1, $int_limit, $int_userid, $str_otherWhere, $int_countrows );

		if( empty( $arr_tradeLogs ) && !empty( $int_countrows ) ){
		    $int_nowPage = 1;
            $arr_tradeLogs = $agentAccountService->getDataByTradeType( $int_tradetype, $int_nowPage - 1, $int_limit, $int_userid, $str_otherWhere, $int_countrows );
		}
		if (!empty($arr_tradeLogs)) {
    		foreach ( $arr_tradeLogs as $int_key => $arr_tradeLog ){
        		$int_tempCostType = $arr_tradeLog['trade_type'] > 1 ? $arr_tradeLog['withdraw_type'] : $arr_tradeLog['supplement_type'];
        		$arr_tradeLogs[$int_key]['about_type'] = ceil( intval( $int_tempCostType, 10 ) / 10 );
    		}
		}

		/**
		 * get trade type
		 */
		$arr_tradeTypes = AgentAccount::getAgentAccountSupplementType();
		if( $int_tradetype > 1 )
            $arr_tradeTypes = AgentAccount::getAgentAccountWithdrawType();

        require_once '../app/views/AccountAgentTradeLogsListView.php';
        $accountAgentTradeLogsListView = new AccountAgentTradeLogsListView();
        $accountAgentTradeLogsListView->setTradeType($int_tradetype);
        $accountAgentTradeLogsListView->tradetypes = $arr_tradeTypes;
        $accountAgentTradeLogsListView->logs = $arr_tradeLogs;
        $arr_select['page'] = $arr_select['page'];
        $accountAgentTradeLogsListView->formdata = $arr_select;
        $accountAgentTradeLogsListView->pager = array('countrows'=> $int_countrows,'limit'=>$int_limit);
        echo $accountAgentTradeLogsListView->getPage();

    }

    /**
     * Select detail trade info and display
     *
     */
    public function tradeloginfo(){
    	$config = $GLOBALS["config"];
        require_once "../". $GLOBALS["local"] ."/"."agentAccount.php"; 
    	AAService::isLogin(true);
        $agentAccountService = new AgentAccountSercive();
		$int_logid = $GLOBALS['config']['urlparam'][3];
		$arr_log = $agentAccountService->getTradeLogById( $int_logid );

		/**
		 * in/out type process
		 */
		$int_tempCostType = $arr_log['trade_type'] > 1 ? $arr_log['withdraw_type'] : $arr_log['supplement_type'];
		$arr_log['about_type'] = ceil( intval( $int_tempCostType, 10 ) / 10 );

    	require_once '../app/views/AccountAgentTradeLogInfoView.php';
    	$accountAgentTradeLogInfoView = new AccountAgentTradeLogInfoView();
    	$accountAgentTradeLogInfoView->log = $arr_log;
    	echo $accountAgentTradeLogInfoView->getPage();
    }

    /**
     * apply supplement
     * @return unknown_type
     */
    public function applypayment(){
    	AAService::isLogin(true);
    	require_once "../". $GLOBALS["local"] ."/"."supplement.php";
    	require_once '../app/views/ApplyPayment.php';
    }

    /**
     * apply withdraw
     * @return unknown_type
     */
    public function applywithdraw(){
    	AAService::isLogin(true);
    	require_once "../". $GLOBALS["local"] ."/"."withdraw.php";
    	require_once '../app/views/ApplyWithdraw.php';
    }


    public function supplysuccess(){
    	require_once "../". $GLOBALS["local"] ."/"."supplement.php";
        require_once '../app/views/SupplySuccess.php';
    }

    /**
     * Show user trade log list
     *
     * @author by M.Wang
     * @return void
     */
    public function mytradelogs(){
    	AAService::isLogin(true);
        $int_userid = AAService::getCustomer();

        $agentAccountService = new AgentAccountSercive();

        /**
         * Select data
         */
        $arr_select = array(
            'page' => HttpUtil::getPostString( 'page' ),
            'createdd' => HttpUtil::getPostString( 'createdd' ),
            'createdu' => HttpUtil::getPostString( 'createdu' ),
            'trade_type' => HttpUtil::getPostString( 'trade_type' )
        );
        $str_otherWhere = '';
        if( !empty( $arr_select['createdd'] ) )
            $str_otherWhere.= " AND `a`.`created` >= '{$arr_select['createdd']}' ";
        if( !empty( $arr_select['createdu'] ) )
            $str_otherWhere.= " AND `a`.`created` <= '{$arr_select['createdu']}' ";
        if( !empty( $arr_select['trade_type'] ) )
            $str_otherWhere.= " AND `a`.`trade_type` = '{$arr_select['trade_type']}' ";

        /**
         * Make page limit
         */
        $int_limit = $GLOBALS['config']['items_per_page'];
        $int_nowPage = $arr_select['page'];
        $int_countrows = 0;
        $arr_tradeLogs = $agentAccountService->getDataByTradeType( -1, $int_nowPage - 1, $int_limit, $int_userid, $str_otherWhere, $int_countrows );

        if( empty( $arr_tradeLogs ) && !empty( $int_countrows ) ){
            $int_nowPage = 1;
            $arr_tradeLogs = $agentAccountService->getDataByTradeType( -1, $int_nowPage - 1, $int_limit, $int_userid, $str_otherWhere, $int_countrows );
        }

        require_once '../app/views/AgentTradeLogsListView.php';
        $accountAgentTradeLogsListView = new AccountAgentTradeLogsListView();
        $accountAgentTradeLogsListView->tradetypes = AgentAccount::getAgentAccountTradeType();
        $accountAgentTradeLogsListView->logs = $arr_tradeLogs;
        $arr_select['page'] = $int_nowPage;
        $accountAgentTradeLogsListView->formdata = $arr_select;
        $accountAgentTradeLogsListView->pager = array('countrows'=> $int_countrows,'limit'=>$int_limit);
        echo $accountAgentTradeLogsListView->getPage();

    }

}


